**************************************************
********* Table A2: Descriptive Statistics of All*
********* State Legislative General Elections,****
********* 1996-2015 ******************************
**************************************************

do "do_files/prep_GRACE_data/load_prep_GRACE_data.do"
do "do_files/prep_GRACE_data/ballot_level_prep.do"

quietly unique ballots_2021
local distinct_elections = r(N)


mat election_winners = J(8,3,0)

local j = 1
local i = 1

local basket_list b_afamwwinner b_afammwinner b_asamwwinner b_asammwinner b_latwwinner b_latmwinner b_whwwinner b_whmwinner

foreach basket of local basket_list{
    quietly count if `basket' == 1
    mat election_winners[`j', `i'] = r(N) / `distinct_elections'
    if `i' == 2 {
        mat election_winners[`j', 3] = election_winners[`j', 1] + election_winners[`j', 2]
        local i = 1
        local `j++'
    }
    else {
        local `i++'
    }
    if `j' == 5{
        mat col_sum_tmp = J(1,8,1)
        mat col_sums =  col_sum_tmp * election_winners
        mat election_winners[`j',1] = col_sums[1,1]
        mat election_winners[`j',2] = col_sums[1,2]
    }
} 

// Note sure which variables to use to count these

quietly count if repind_win == 1 & woman_win==1
mat election_winners[6, 1] = r(N) / `distinct_elections'
count if repind_win == 1 & woman_win==0
mat election_winners[6, 2] = r(N) / `distinct_elections'
mat election_winners[6, 3] = election_winners[6, 1] + election_winners[6, 2]

quietly count if inc_win == 1 & woman_win==1
mat election_winners[7, 1] = r(N) / `distinct_elections'
count if inc_win == 1 & woman_win==0
mat election_winners[7, 2] = r(N) / `distinct_elections'
mat election_winners[7, 3] = election_winners[7, 1] + election_winners[7, 2]

quietly count if inc_win == 0 & woman_win==1
mat election_winners[8, 1] = r(N) / `distinct_elections'
count if inc_win == 0 & woman_win==0
mat election_winners[8, 2] = r(N) / `distinct_elections'
mat election_winners[8, 3] = election_winners[8, 1] + election_winners[8, 2]

mat rownames election_winners = "African American" "Asian American" "Latina/o" "White" "Total" "Republican or Independent" "Incumbent" "Challenger"
mat colnames election_winners = "Women" "Men" "Total"
mat list election_winners

* pausing for users who might be looping through all replication files
pause

mat election_char_table = J(7,1,0)
local i = 1
// count if v07 == 8
local election_characteristics v07 allincfmt openelect multimem unemppct educcolldeg demvoteshareballot_2020
foreach characteristic of local election_characteristics {
    if "`characteristic'" == "v07"{
        quietly count if v07 == 8
        mat election_char_table[`i', 1] = r(N) / `distinct_elections'
    } 
    else {
        quietly sum `characteristic'
        mat election_char_table[`i', 1] = r(mean)
    }
    local `i++'
}

mat rownames election_char_table = "Upper Chamber" "Income (10k)" "Open Seat" " Multi-Member District"  "Unemployment" "Percent College Education" "Democratic Vote Share"
mat colnames election_char_table = "Means"
mat list election_char_table
* pausing for users who might be looping through all replication files
pause

mat repind_share_wins = J(4,4,0)
local i = 1
local j = 1
// summarize by group for getting repind_win 
foreach basket of local basket_list{
    quietly count if `basket' == 1
    local total_wins = r(N)
    quietly count if `basket' == 1 & repind_win == 1
    if mod(`i', 2) != 0{
        mat repind_share_wins[`i', `j'] = r(N) / `total_wins'
        mat repind_share_wins[`i', `j'+2] = `total_wins'
    }
    else{
        mat repind_share_wins[`i', `j'] = r(N) / `total_wins'
        mat repind_share_wins[`i', `j'+2] = `total_wins'
        
    }
    if `j' == 2{
        local `j--'
        local `i++'
    } 
    else {
        local `j++'
    }
    
    
    
}

mat rownames repind_share_wins = "African American" "Asian American" "Latina/o" "White"
mat colnames repind_share_wins = "Women" "Men" "Women Total # Wins" "Men Total # Wins"
mat list repind_share_wins

* pausing for users who might be looping through all replication files
pause

